<?php

namespace app\common\model;

use think\Model;

class ProfessionCourse extends Model
{

    /**
     * getValueById
     * @param $id
     * @param $field
     * @return mixed
     */
    public function getValueById($id, $field)
    {
        $value = $this->where('id', $id)->value($field);
        return $value;
    }

    /**
     * 计算考试倒计时天数
     * @param $professionCourseId
     * @return int
     */
    public function getExamCountdown($professionCourseId)
    {
        if (!$professionCourseId) {
            return 0;
        }

        // 获取题库考试时间
        $examTime = $this->getValueById($professionCourseId, 'exam_time');
        if (!$examTime) {
            return 0;
        }
        
        $daysLeft = floor(($examTime - time()) / 86400);
        
        // 如果天数小于0，则返回0
        return $daysLeft < 0 ? 0 : (int)$daysLeft;
    }
}
